package com.start.form.engine.core.global;

import com.alibaba.fastjson2.JSONObject;
import com.start.common.dataRule.StartQueryWrapper;
import com.start.common.std.StartEventDispatcherListener;
import com.start.common.std.StartSingletonFactory;
import com.start.common.tools.SpringContextTools;
import com.start.form.engine.core.entity.StartFormConfig;
import com.start.form.engine.core.listener.StartTableDataListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 表数据分发器
 */
@Slf4j
public class DispatcherStartFormDataListener extends StartEventDispatcherListener<DispatcherStartFormDataListener, StartTableDataListener> implements StartTableDataListener {




    /**
     * 获取实例对象
     *
     * @return
     */
    public static DispatcherStartFormDataListener getInstance() {
        return StartSingletonFactory.get(DispatcherStartFormDataListener.class);
    }


    /**
     * 初始化分发器
     */
    @Override
    protected void initDispatcher() {
        Map<String, StartTableDataListener> beans = SpringContextTools.getBeans(StartTableDataListener.class, false);
        if (beans == null || beans.isEmpty()) {
            return;
        }
        Collection<StartTableDataListener> values = beans.values();
        for (StartTableDataListener listener : values) {
            if (StringUtils.isNotEmpty(listener.getTableName())) {
                addEvent(UNION_KEY_PREFIX, listener.getTableName(), listener);
                continue;
            }
            if (StringUtils.isNotEmpty(listener.getGroupCode())) {
                addEvent(GROUP_PREFIX, listener.getGroupCode(), listener);
                continue;
            }
            addEvent(GLOBAL_PREFIX, GLOBAL_KEY, listener);
        }

    }


    /**
     * 保存数据前
     *
     * @param formConfig 数据配置
     * @param data       数据
     */
    @Override
    public void beforeSaveData(StartFormConfig formConfig, JSONObject data) {

        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeSaveData(formConfig, data);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeSaveData(formConfig, data);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeSaveData(formConfig, data);
            }
        }
    }

    /**
     * 保存数据后
     *
     * @param formConfig 表配置
     * @param data       保存的数据
     */
    @Override
    public void afterSaveData(StartFormConfig formConfig, JSONObject data) {
        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterSaveData(formConfig, data);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterSaveData(formConfig, data);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterSaveData(formConfig, data);
            }
        }
    }

    /**
     * 修改数据前
     *
     * @param formConfig 表配置
     * @param data       数据
     * @param wrapper
     */
    @Override
    public void beforeUpdate(StartFormConfig formConfig, JSONObject data, StartQueryWrapper wrapper) {
        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeUpdate(formConfig, data, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeUpdate(formConfig, data, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeUpdate(formConfig, data, wrapper);
            }
        }
    }

    /**
     * 修改数据后
     *
     * @param formConfig 表配置
     * @param data       数据
     * @param wrapper    条件
     */
    @Override
    public void afterUpdate(StartFormConfig formConfig, JSONObject data, StartQueryWrapper wrapper) {

        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterUpdate(formConfig, data, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterUpdate(formConfig, data, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterUpdate(formConfig, data, wrapper);
            }
        }
    }

    /**
     * 删除数据前
     *
     * @param formConfig 表配置
     * @param wrapper    条件
     */
    @Override
    public void beforeDelete(StartFormConfig formConfig, StartQueryWrapper wrapper) {
        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeDelete(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeDelete(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeDelete(formConfig, wrapper);
            }
        }
    }

    /**
     * 删除数据后
     *
     * @param formConfig 表配置
     * @param wrapper    条件
     */
    @Override
    public void afterDelete(StartFormConfig formConfig, StartQueryWrapper wrapper) {
        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterDelete(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterDelete(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterDelete(formConfig, wrapper);
            }
        }
    }

    /**
     * 数据查询前
     *
     * @param formConfig 数据表配置
     * @param wrapper    条件
     */
    @Override
    public void beforeQueryData(StartFormConfig formConfig, StartQueryWrapper wrapper) {

        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeQueryData(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeQueryData(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeQueryData(formConfig, wrapper);
            }
        }
    }

    /**
     * 数据查询后
     *
     * @param formConfig 表配置
     * @param wrapper    条件
     * @param records    数据
     */
    @Override
    public void afterQueryData(StartFormConfig formConfig, StartQueryWrapper wrapper, JSONObject... records) {

        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterQueryData(formConfig, wrapper, records);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterQueryData(formConfig, wrapper, records);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterQueryData(formConfig, wrapper, records);
            }
        }
    }

    /**
     * 查询数量之前
     *
     * @param formConfig 数据表配置
     * @param wrapper    查询条件
     */
    @Override
    public void beforeCount(StartFormConfig formConfig, StartQueryWrapper wrapper) {
        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeCount(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeCount(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.beforeCount(formConfig, wrapper);
            }
        }
    }

    /**
     * 查询数量之后
     *
     * @param formConfig 数据表配置
     * @param wrapper    查询条件
     */
    @Override
    public void afterCount(StartFormConfig formConfig, StartQueryWrapper wrapper) {
        List<StartTableDataListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterCount(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterCount(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableDataListener listener : listeners) {
                listener.afterCount(formConfig, wrapper);
            }
        }
    }

}
